package io.itit.ecp.admin.provide.action.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.itit.ecp.admin.provide.action.req.CarbonEmissionRangeQueryReq;
import io.itit.ecp.admin.provide.renderer.KCarbonEmissionRangeRenderer;
import io.itit.ecp.admin.server.entity.CarbonEmissionRangeEntity;
import io.itit.ecp.admin.server.service.ICarbonEmissionRangeService;
import io.itit.grass.common.provide.action.req.BaseBatchIdReq;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IAdd;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.provide.action.validation.IRequired;
import io.itit.grass.common.provide.action.validation.IUpdate;
import io.itit.grass.common.server.utils.ConvertUtils;
import io.itit.grass.estelle.starter.parse.ExcelParseManage;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Api(tags = "碳排放范围边界")
@RestController
@RequestMapping("/ecp-admin/carbonEmissionRange")
public class CarbonEmissionRangeController {

    @Resource
    private ICarbonEmissionRangeService carbonEmissionRangeService;

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    @RequiresPermissions("ecp-admin:carbonEmissionRange:search")
    public ResultResp<IPage<CarbonEmissionRangeEntity>> queryPage(@RequestBody @Validated(IQueryPage.class) CarbonEmissionRangeQueryReq req) {
        Page<CarbonEmissionRangeEntity> page = carbonEmissionRangeService.page(PageUtils.getPage(req, CarbonEmissionRangeEntity.class), buildQW(req));
        return ResultResp.success(ConvertUtils.convert(page, CarbonEmissionRangeEntity.class));
    }

    @ApiOperation("详情")
    @PostMapping("detail")
    @RequiresPermissions("ecp-admin:carbonEmissionRange:search")
    public ResultResp<CarbonEmissionRangeEntity> detail(@RequestBody @Validated(IRequired.class) BaseIdReq req) {
        return ResultResp.success(ConvertUtils.copy(carbonEmissionRangeService.getById(req.getId()), CarbonEmissionRangeEntity.class));
    }

    @ApiOperation("新增")
    @PostMapping("add")
    @RequiresPermissions("ecp-admin:carbonEmissionRange:add")
    public ResultResp<Boolean> add(@RequestBody @Validated({IAdd.class}) CarbonEmissionRangeEntity req) {
        return ResultResp.success(carbonEmissionRangeService.saveOrUpdate(req));
    }

    @ApiOperation("编辑")
    @PostMapping("update")
    @RequiresPermissions("ecp-admin:carbonEmissionRange:update")
    public ResultResp<Boolean> update(@RequestBody @Validated({IUpdate.class}) CarbonEmissionRangeEntity req) {
        return ResultResp.success(carbonEmissionRangeService.updateById(req));
    }

    @ApiOperation("删除(根据Id删)")
    @PostMapping("deleteById")
    @RequiresPermissions("ecp-admin:carbonEmissionRange:deleteById")
    public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
        return ResultResp.success(carbonEmissionRangeService.removeById(req.getId()));
    }

    @ApiOperation("批量删除")
    @PostMapping("batchDelete")
    @RequiresPermissions("ecp-admin:carbonEmissionRange:batchDelete")
    public ResultResp<Boolean> batchDelete(@RequestBody @Validated({IRequired.class}) BaseBatchIdReq req) {
        return ResultResp.success(carbonEmissionRangeService.removeBatchByIds(req.getIds(), 1000));
    }

    @ApiOperation("导入")
    @PostMapping("upload")
    @RequiresPermissions("ecp-admin:carbonEmissionRange:upload")
    public ResultResp<Integer> upload(@RequestParam("file") MultipartFile file, Boolean ignoreError) {
        return ResultResp.success(ExcelParseManage.i().process(KCarbonEmissionRangeRenderer.ENTITY_NAME, file, ignoreError));
    }

    private Wrapper<CarbonEmissionRangeEntity> buildQW(CarbonEmissionRangeQueryReq req) {
        return Wrappers.<CarbonEmissionRangeEntity>lambdaQuery()
                .like(StringUtils.hasLength(req.getRangeName()), CarbonEmissionRangeEntity::getRangeName, req.getRangeName())
                .orderByDesc(CarbonEmissionRangeEntity::getCreateTm);
    }
}
